package cn.sxw.ld.module.shoppingcat.controller;

import cn.sxw.ld.common.dto.R;
import cn.sxw.ld.module.shoppingcat.pojo.ShoppingCart;
import cn.sxw.ld.module.shoppingcat.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:ShoppingCartController
 * Package:cn.sjxy.graduation.controller
 * Description:
 *
 * @Date:2022/6/25 14:29
 * @Author:SXW
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        List<ShoppingCart> carts = shoppingCartService.getCart();
        return R.success(carts);
    }

    /**
     * 添加一件商品到购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart cart = shoppingCartService.add(shoppingCart);
        return R.success(cart);
    }

    /**
     * 减少一份
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart cart = shoppingCartService.sub(shoppingCart);
        return R.success(cart);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clearShoppingCart() {
        shoppingCartService.clearShoppingCart();
        return R.success("删除失败！");
    }


}
